//
//  MinePhotoCollectionViewCell.swift
//  MT
//
//  Created by M on 2019/12/25.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory
import Kingfisher

class MinePhotoCollectionViewCell: UICollectionViewCell {
    
    /// 是否最后一个 展示的 Cell
    var remainCount: Int = 0 {
        didSet {
            remainImageLabel.isHidden = remainCount == 0
            remainImageLabel.text = "+\(remainCount)"
        }
    }
    
    /// 媒体
    var model: UserMediaListModel? {
        didSet {
            guard let coverImage = model?.coverImg else {
                if let href = model?.href {
                    imgView.kf.setImage(with: URL(string: href), placeholder: UIImage(named: "placeholder-rectangle"))
                }
                return
            }
            imgView.kf.setImage(with: URL(string: coverImage), placeholder: UIImage(named: "placeholder-rectangle"))
        }
    }
    
    /// 图片
    lazy var imgView: UIImageView = {
        let imgView = UIImageView(frame: self.contentView.bounds)
        imgView.contentMode = .scaleAspectFill
        imgView.layer.cornerRadius = 4
        imgView.clipsToBounds = true
        return imgView
    }()
    
    /// 剩余图片 Label
    lazy var remainImageLabel: UILabel = {
        let label = UILabel(frame: self.contentView.bounds)
        label.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        label.font = PingFangSCSemiboldFont(16)
        label.textColor = .white
        label.textAlignment = .center
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ Overrides ----------------------
extension MinePhotoCollectionViewCell {
    
    override func prepareForReuse() {
        remainImageLabel.isHidden = true
        imgView.image = nil
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension MinePhotoCollectionViewCell {
    
    private func initializeUI() {
        contentView.addSubview(imgView)
        contentView.addSubview(remainImageLabel)
        imgView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        remainImageLabel.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
    }
    
}
